﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel.Day
{
    public class GroupShow
    {
        /// <summary>
        /// 左侧菜单显示信息
        /// </summary>
        public LeftMenuInfos LeftMenuInfos { get; set; }

        public string HPdate { get; set; }

        public Models.Sys.Groups Group { get; set; }

        public DateTime Pdate { get; set; }

        public string GroupQueryDate { get; set; }

        public List<Models.View.OperatorDayChange> OperatorDayChange { get; set; }

        public List<Models.View.OperatorWeekSum> OperatorWeekSum { get; set; }

        public List<Models.View.OperatorMonthSum> OperatorMonthSum { get; set; }

        public List<Models.View.OperatorWeekExpected> OperatorWeekExpected { get; set; }

        public Models.Week.GroupWeekSum GroupWeekSum { get; set; }

        public Models.Day.GroupDayChange GroupDayChange { get; set; }

        public Models.Year.GroupAvgYear GroupAvgYear { get; set; }

        public Models.View.GroupFiveDayAvg GroupFiveDayAvg { get; set; }

        public Models.Month.GroupAvgMonth GroupAvgMonth { get; set; }

        public Models.Day.GroupAvgDay GroupDayAvg { get; set; }

        public Models.Day.CompanyAvgDay CompanyAvgDay { get; set; }

        public Models.Month.GroupMonthSum GroupMonthSum { get; set; }

        public Models.Year.GroupYearSum GroupYearSum { get; set; }

        public string JS { get; set; }


        #region 定义一些使用到的接口
        private Interface.Sys.IGroups IGroups = null;
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.Sys.IDayGroupOperators IDayGroupOperators = null;
        private Interface.View.IOperatorDayChange IOperatorDayChange = null;
        private Interface.View.IOperatorWeekSum IOperatorWeekSum = null;
        private Interface.View.IOperatorMonthSum IOperatorMonthSum = null;
        private Interface.View.IOperatorWeekExpected IOperatorWeekExpected = null;
        private Interface.Day.IGroupDayChange IGroupDayChange = null;
        private Interface.Year.IGroupAvgYear IGroupAvgYear = null;
        private Interface.View.IGroupFiveDayAvg IGroupFiveDayAvg = null;
        private Interface.Month.IGroupAvgMonth IGroupAvgMonth = null;
        private Interface.Day.IGroupAvgDay IGroupAvgDay = null;
        private Interface.Day.ICompanyAvgDay ICompanyAvgDay = null;
        private Interface.Week.IGroupWeekSum IGroupWeekSum = null;
        private Interface.Month.IGroupMonthSum IGroupMonthSum = null;
        private Interface.Year.IGroupYearSum IGroupYearSum = null;
        #endregion


        public GroupShow(int groupId, int queryDate, DB.DBManager db)
        {
            InitInterface(db);

            this.Group = IGroups.GetGroup(groupId);

            string _year = queryDate.ToString().Substring(0, 4);
            string _month = queryDate.ToString().Substring(4, 2);
            string _day = queryDate.ToString().Substring(6, 2);
            this.GroupQueryDate = _year + "-" + _month + "-" + _day;
            this.Pdate = Convert.ToDateTime(this.GroupQueryDate);

            Models.Sys.TradeHistory th = ITradeHistory.GetSettlementModel(queryDate.ToString());

            List<Models.Sys.Operator> operators = IDayGroupOperators.GetGroupOperators(this.Group.Id, this.Pdate);
            List<Models.View.OperatorDayChange> changes = new List<Models.View.OperatorDayChange>();
            foreach (Models.Sys.Operator op in operators)
            {
                Models.View.OperatorDayChange change = IOperatorDayChange.GetOperatorDayChange(op.Id, th.Id);
                changes.Add(change);
            }
            this.OperatorDayChange = changes;


            //周累计
            List<Models.View.OperatorWeekSum> weekchanges = new List<Models.View.OperatorWeekSum>();
            foreach (Models.Sys.Operator op in operators)
            {
                Models.View.OperatorWeekSum change = IOperatorWeekSum.GetOperatorWeekSum(op.Id, th.YPdate, th.MPdate, th.WPdate);
                weekchanges.Add(change);
            }
            this.OperatorWeekSum = weekchanges;


            //月累计
            List<Models.View.OperatorMonthSum> monthchanges = new List<Models.View.OperatorMonthSum>();
            foreach (Models.Sys.Operator op in operators)
            {
                Models.View.OperatorMonthSum change = IOperatorMonthSum.GetOperatorMonthSum(op.Id, th.MPdate, th.YPdate);
                monthchanges.Add(change);
            }
            this.OperatorMonthSum = monthchanges;


            //周预期
            List<Models.View.OperatorWeekExpected> weekexpectedchanges = new List<Models.View.OperatorWeekExpected>();
            foreach (Models.Sys.Operator op in operators)
            {
                Models.View.OperatorWeekExpected change = IOperatorWeekExpected.GetOperatorWeekExpected(op.Id, th.WPdate, th.YPdate);
                weekexpectedchanges.Add(change);
            }
            this.OperatorWeekExpected = weekexpectedchanges;

            this.GroupDayChange = IGroupDayChange.GetGroupDayChange(this.Group.Id, th.Id);//保存当日交易数据供页面调用

            //保存小组年均值数据供页面调用
            this.GroupAvgYear = IGroupAvgYear.GetGroupAvgYear(this.Group.Id, th.YPdate);//查询小组年均值ok

            this.GroupFiveDayAvg = IGroupFiveDayAvg.GetGroupFiveDayAvg(this.Group.Id, th.Id);//五日均值ok

            this.GroupAvgMonth = IGroupAvgMonth.GetGroupAvgMonth(this.Group.Id, th.MPdate, th.YPdate);//当月均值ok

            this.GroupDayAvg = IGroupAvgDay.GetGroupAvgDay(this.Group.Id, th.Id);//查询小组均值

            this.CompanyAvgDay = ICompanyAvgDay.GetCompanyAvgDay(th.Id);//查询公司均值

            this.GroupWeekSum = IGroupWeekSum.GetGroupWeekSum(this.Group.Id, th.WPdate, th.YPdate);//查询小组周累计

            this.GroupMonthSum = IGroupMonthSum.GetGroupMonthSum(this.Group.Id, th.MPdate, th.YPdate);//查询小组月累计ok

            this.GroupYearSum = IGroupYearSum.GetGroupYearSum(this.Group.Id, th.YPdate);//查询小组年累计ok

            this.JS += Benefit.Tools.ChartAtManager.ChartManager.GroupBar(this.OperatorDayChange);
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.GroupDayDiffBar(this.OperatorDayChange);

            DisposeInterface();
        }
        /// <summary>
        /// 初始化接口
        /// </summary>
        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            IGroups = new Service.Sys.ServiceGroups(db);
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IDayGroupOperators = new Service.Sys.ServiceDayGroupOperators(db);
            IOperatorDayChange = new Service.View.ServiceOperatorDayChange(db);
            IOperatorWeekSum = new Service.View.ServiceOperatorWeekSum(db);
            IOperatorMonthSum = new Service.View.ServiceOperatorMonthSum(db);
            IOperatorWeekExpected = new Service.View.ServiceOperatorWeekExpected(db);
            IGroupDayChange = new Service.Day.ServiceGroupDayChange(db);
            IGroupAvgYear = new Service.Year.ServiceGroupAvgYear(db);
            IGroupFiveDayAvg = new Service.View.ServiceGroupFiveDayAvg(db);
            IGroupAvgMonth = new Service.Month.ServiceGroupAvgMonth(db);
            IGroupAvgDay = new Service.Day.ServiceGroupAvgDay(db);
            ICompanyAvgDay = new Service.Day.ServiceCompanyAvgDay(db);
            IGroupWeekSum = new Service.Week.ServiceGroupWeekSum(db);
            IGroupMonthSum = new Service.Month.ServiceGroupMonthSum(db);
            IGroupYearSum = new Service.Year.ServiceGroupYearSum(db);
            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            IGroups = null;
            ITradeHistory = null;
            IDayGroupOperators = null;
            IOperatorDayChange = null;
            IOperatorWeekSum = null;
            IOperatorMonthSum = null;
            IOperatorWeekExpected = null;
            IGroupDayChange = null;
            IGroupAvgYear = null;
            IGroupFiveDayAvg = null;
            IGroupAvgMonth = null;
            IGroupAvgDay = null;
            ICompanyAvgDay = null;
            IGroupWeekSum = null;
            IGroupMonthSum = null;
            IGroupYearSum = null;
            #endregion
        }
    }
}
